#include "stdafx.h"
#include "jwindow.h"
#include "skins.h"

#include <gtl/script/jscript.h>

namespace gtl
{

namespace skins
{

skins* jwindow::m_skins = NULL;

jwindow::jwindow()
{
	printf("construct jwindow\n");
}

v8::Handle<v8::Value> jwindow::close(const v8::Arguments& args)
{
	return v8::Undefined();
}

v8::Handle<v8::Value> jwindow::show(const v8::Arguments& args)
{
	return v8::Undefined();
}

v8::Handle<v8::Value> jwindow::move(const v8::Arguments& args)
{
	return v8::Undefined();
}

v8::Handle<v8::Value> jwindow::alert(const v8::Arguments& args)
{
	if(args.Length() <= 0)
		return v8::False();

	v8::String::Utf8Value str(args[0]);
	const char* text = gtl::jscript::cstr(str);

	const char* caption = NULL;
	if(args.Length() >= 2)
	{
		v8::String::Utf8Value str(args[1]);
		caption = gtl::jscript::cstr(str);
	}

	HWND hWnd = NULL;
	char szCaption[MAX_PATH] = {0};
	if(m_skins != NULL && m_skins->get_main_wnd() != NULL)
	{
		hWnd = m_skins->get_main_wnd()->m_hWnd;
		if(hWnd != NULL && caption == NULL)
		{
			::GetWindowTextA(hWnd, szCaption, MAX_PATH);
			caption = szCaption;
		}
	}

	::MessageBoxA(hWnd, text, caption, MB_OK);

	return v8::True();
}

v8::Handle<v8::Value> jwindow::visible(const v8::Arguments& args)
{
	return v8::False();
}

v8::Handle<v8::Value> jwindow::hwnd(const v8::Arguments& args)
{
	return v8::Integer::New((int32_t)get_hwnd(args));
}

v8::Handle<v8::Value> jwindow::position(const v8::Arguments& args)
{
	v8::Local<v8::Array> pos = v8::Array::New(2);
	pos->Set(0, v8::Integer::New(0));
	pos->Set(1, v8::Integer::New(0));

	return pos;
}

v8::Handle<v8::Value> jwindow::width(const v8::Arguments& args)
{
	return v8::Integer::New(0);
}

v8::Handle<v8::Value> jwindow::height(const v8::Arguments& args)
{
	return v8::Integer::New(0);
}

v8::Handle<v8::Value> jwindow::rect(const v8::Arguments& args)
{
	v8::Local<v8::Array> rc = v8::Array::New(4);
	rc->Set(0, v8::Integer::New(0));
	rc->Set(1, v8::Integer::New(0));
	rc->Set(2, v8::Integer::New(0));
	rc->Set(3, v8::Integer::New(0));

	return rc;
}

void jwindow::set_skins_ptr(skins* skinsptr)
{
	m_skins = skinsptr;
}

HWND jwindow::get_hwnd(const v8::Arguments& args)
{
	if(m_skins == NULL)
		return NULL;

	if(args.Length() <= 0)
		return NULL;

	v8::String::Utf8Value str(args[0]);
	gtl::str id = gtl::jscript::cstr(str);

#ifdef UNICODE
	wnd* wnd_ptr = m_skins->get_ctrl<wnd>(id.trans());
#else
	wnd* wnd_ptr = m_skins->get_ctrl<wnd>(id);
#endif
	if(wnd_ptr == NULL)
		return NULL;

	return wnd_ptr->m_hWnd;
}

} // end of namespace skins

} // end of namespace gtl
